package com.huike.business.service.impl;


import com.huike.business.domain.TbBusiness;
import com.huike.business.domain.TbBusinessTrackRecord;
import com.huike.business.domain.vo.BusinessTrackVo;
import com.huike.business.mapper.TbBusinessMapper;
import com.huike.business.mapper.TbBusinessTrackRecordMapper;
import com.huike.business.service.ITbBusinessTrackRecordService;
import com.huike.clues.mapper.TbClueTrackRecordMapper;
import com.huike.clues.service.ISysDictDataService;
import com.huike.common.utils.SecurityUtils;
import com.huike.common.utils.bean.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 商机跟进记录Service业务层处理
 * 
 * @author wgl
 * @date 2021-04-28
 */
@Service
public class TbBusinessTrackRecordServiceImpl implements ITbBusinessTrackRecordService {
    @Autowired
    TbBusinessMapper tbBusinessMapper;
    @Autowired
    TbBusinessTrackRecordMapper tbBusinessTrackRecordMapper;
    @Autowired
    ISysDictDataService sysDictDataService;

    @Override
    public void addBusinessTrack(BusinessTrackVo businessTrackVo) {
        TbBusiness tbBusiness = new TbBusiness();
        BeanUtils.copyProperties(businessTrackVo, tbBusiness);
        tbBusiness.setId(businessTrackVo.getBusinessId());
        tbBusinessMapper.updateTbBusiness(tbBusiness);

        String username = SecurityUtils.getUsername();
        tbBusinessTrackRecordMapper.addBusinessTrack(businessTrackVo, username);


    }

    @Override
    public List<TbBusinessTrackRecord> selectBusinessTrack(Long id) {
        //思考:因为tbBusinessTrackRecordMapper获取的对象集合不包括返回需要的keys字段,所以遍历对象,将对象的KeyItems值查询对应的keys
        //1.获取TbBusinessTrackRecord集合
        List<TbBusinessTrackRecord> tbBusinessTrackRecordList = tbBusinessTrackRecordMapper.selectBusinessTrack(id);

        //2.遍历集合
        for (TbBusinessTrackRecord tbBusinessTrackRecord : tbBusinessTrackRecordList) {
            //2.将tbBusinessTrackRecord对象中的keyItems取出
            String[] split = tbBusinessTrackRecord.getKeyItems().split(",");
            //3.将tbBusinessTrackRecord对象中的keys取出
            List<String> keys = tbBusinessTrackRecord.getKeys();

            //3.将split赋值给tbBusinessTrackRecord中的keys
            for (String s : split) {
                String key = sysDictDataService.selectDictLabel("communication_point", s);//通过sysDictDataService获取type(communication_point)和value(2,4,5)对应的label(价格,师资....)
                keys.add(key);
            }
        }
        return tbBusinessTrackRecordList;

    }
}
